import '../utils/tools.dart';

import 'comparable.dart';
import 'sku_model.dart';
import 'image_model.dart';

class ProductModel implements Equable<int> {
  int id = 0;
  String? lang;
  int mainId = 0;
  int cateId = 0;
  int brandId = 0;

  String title = '';
  String viceTitle = '';
  String goodsNo = '';
  String image = '';

  List<ImageModel>? images;
  List<SkuModel>? skus;
  Map<String, dynamic>? specData;
  Map<String, dynamic>? propData;

  double maxPrice = 0;
  double minPrice = 0;
  double marketPrice = 0;

  String content = '';

  int createTime = 0;
  int updateTime = 0;
  int levelId = 0;
  List<int>? levels;

  int storage = 0;
  int postageId = 0;
  double postage = 0;

  int sale = 0;
  int type = 0;

  int isCommission = 0;
  int isDiscount = 0;
  int status = 0;

  dynamic commissionPercent;

  ProductModel();

  ProductModel.fromJson(Map? product) {
    if (product == null || !product.containsKey('id')) return;

    id = product['id'];
    cateId = product['cate_id'];
    brandId = product['brand_id'];
    title = product['title'];
    viceTitle = product['vice_title'];
    image = Tools.fixImageUrl(product['image']);

    maxPrice = product['max_price'];
    minPrice = product['min_price'];

    content = Tools.fixContentImageUrl(product['content']) ?? '';

    if (product.containsKey('prop_data') && product['prop_data'] is Map) {
      propData = product['prop_data'];
    }
    if (product.containsKey('spec_data') && product['spec_data'] is Map) {
      specData = product['spec_data'];
    }

    List<dynamic>? images = product['images'];
    if (images != null && images.isNotEmpty) {
      this.images = images
          .map((img) => ImageModel.fromJson(img, type: 'product'))
          .toList();
    }

    List<dynamic>? skus = product['skus'];
    if (skus != null && skus.isNotEmpty) {
      this.skus = skus.map((img) => SkuModel.fromJson(img)).toList();
    }
  }

  @override
  int get compareField => id;
}
